<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="姓名" name="jobNum">
						<a-input v-model:value="searchFormState.name" placeholder="请输入姓名" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="姓名" name="jobNum">
						<a-tree-select
							v-model:value="searchFormState.orgId"
							style="width: 100%"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							placeholder="请选择"
							allow-clear
							tree-default-expand-all
							:tree-data="treeData"
							:tree-default-expanded-keys="treeDefaultExpandedKeys"
							:field-names="{
								children: 'children',
								label: 'name',
								value: 'id'
							}"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="impExpRef.onOpen()" v-if="hasPerm('holidayImport')">
						<template #icon><import-outlined /></template>
						<span>{{ $t('common.imports') }}</span>
					</a-button>
					<a-button @click="exportBatchHoliday" v-if="hasPerm('holidayBatchExport')">
						<template #icon><export-outlined /></template>
						{{ $t('user.batchExportButton') }}
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('sysHolidayAccountEdit')">查看明细</a>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
	<imp-exp ref="impExpRef" @successful="table.refresh(true)" />
</template>

<script setup name="holidayaccount">
	import Form from './form.vue'
	import impExp from './impExp.vue'
	import sysHolidayAccountApi from '@/api/biz/sysHolidayAccountApi'
	import userApi from '@/api/sys/userApi'
	import tool from '@/utils/tool'
	import downloadUtil from '@/utils/downloadUtil'
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const impExpRef = ref()
	const treeDefaultExpandedKeys = ref([])
	const treeData = ref([])
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '姓名',
			dataIndex: 'name',
			width: '250px'
		},
		{
			title: '工号',
			dataIndex: 'userId',
			width: '250px'
		},
		{
			title: '年假余额',
			dataIndex: 'annualLeaveBalance'
		},
		{
			title: '调休假',
			dataIndex: 'compensatoryLeaveBalance'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['sysHolidayAccountEdit', 'sysHolidayAccountDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	nextTick(() => {
		// 机构选择器数据
		userApi.userOrgTreeSelector().then((res) => {
			if (res !== null) {
				treeData.value = res
				// 默认展开2级
				treeData.value.forEach((item) => {
					// 因为0的顶级
					if (item.parentId === '0') {
						treeDefaultExpandedKeys.value.push(item.id)
						// 取到下级ID
						if (item.children) {
							item.children.forEach((items) => {
								treeDefaultExpandedKeys.value.push(items.id)
							})
						}
					}
				})
			}
		})
	})
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return sysHolidayAccountApi.sysHolidayAccountPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	// 批量导出
	const exportBatchHoliday = (record) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		if (selectedRowKeys.value.length > 0) {
			searchFormParam.id = selectedRowKeys.value
				.map((m) => {
					return m
				})
				.join()
		}
		sysHolidayAccountApi.holidayExport(searchFormParam).then((res) => {
			downloadUtil.resultDownload(res)
			table.value.clearSelected()
		})
	}
</script>
